class StoriesController < ApplicationController
  before_filter :login_required,  :except => [:index, :show, :report, :flag_line, :remove_flag, :fetch_earlier]
  before_filter :check_user,      :only => [:edit, :update, :destroy]
  after_filter :store_location,   :only => [:index, :new, :show, :edit]
    
  @@lines_fetched = 6
  
  def print
    @story = Story.find(params[:id])
  end
  
  def report
    @stories = Story.find(:all)
  end
  
  def flag_line
    @flagged_line = Line.find(params[:line_id])
    @flagged_line.update_attributes(:inappropriate=>"true")
    @flagged_line.update_attributes(:reporting_user => params[:user_id])
    render :text=>"line reported"
  end
  
  def remove_flag
    @flagged_line = Line.find(params[:line_id])
    @flagged_line.update_attributes(:inappropriate=>"false")
    @flagged_line.update_attributes(:reporting_user=>"")
    redirect_to "/stories/report/1"
  end
  
  def minimum(a,b)
    if (a < b)
      a
    else
      b
    end
  end
  
  def fetch_earlier
    @story = Story.find(params[:story_id])
    @lines_retrieved = @story.lines[0, @story.lines.length - @@lines_fetched]
    if @@lines_fetched < @story.lines.length
      render :partial => "earlier_lines"
    else
      render :text=> "No earlier lines to display."
    end
  end
  
  def show_descrips
    render :partial => "story_descriptions"
  end
  
  def hide_descrips
    render :partial=> "hide_descriptions"
  end
  
  # GET /stories
  # GET /stories.xml
  def index
    @stories = Story.find(:all, :order=>'created_at desc')

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @stories }
    end
  end

  # GET /stories/1
  # GET /stories/1.xml
  def show
    @story = Story.find(params[:id])
    @line = Line.new
    @tags = @story.tags

    if request.xml_http_request?
      render :partial => "lines"
    else

      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @story }
      end
    end
  end

  # GET /stories/new
  # GET /stories/new.xml
  def new
    @story = Story.new
    @tags = Tag.find(:all)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @story }
    end
  end

  # GET /stories/1/edit
  def edit
#    @story = Story.find(params[:id])
    @tags = Tag.find(:all)
  end

  # POST /stories
  # POST /stories.xml
  def create
    @story = Story.new(params[:story])
    #@story.type = "limerick"
    @story.user = current_user
    @tags = Tag.find(:all)
    if params[:tag_ids]
      @story.tags = Tag.find(params[:tag_ids])
    else
      @story.tags = []
    end

    respond_to do |format|
      if @story.save
        flash[:notice] = 'Story was successfully created.'
        format.html { redirect_to(@story) }
        format.xml  { render :xml => @story, :status => :created, :location => @story }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @story.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stories/1
  # PUT /stories/1.xml
  def update
 #   @story = Story.find(params[:id])
    @tags = Tag.find(:all)
    minimum(1,2)
    if params[:tag_ids]
      @story.tags = Tag.find(params[:tag_ids])
    else
      @story.tags = []
    end

    respond_to do |format|
      if @story.update_attributes(params[:story])
        flash[:notice] = 'Story was successfully updated.'
        format.html { redirect_to(@story) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @story.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stories/1
  # DELETE /stories/1.xml
  def destroy
#    @story = Story.find(params[:id])
    if !@story.lines.nil? && @story.lines.empty?
      @story.destroy

      respond_to do |format|
        format.html { redirect_to(stories_url) }
        format.xml  { head :ok }
      end
    else
      flash[:notice] = 'There are still lines in that story.'
      redirect_back_or_default(stories_url)
    end
  end
  
  protected
  
  def check_user
    @story = Story.find(params[:id])
    if @story.user_id != current_user.id && !current_user.moderator
      access_denied
    end
  end
  
  def access_denied
    if logged_in? && !current_user.moderator
      flash[:notice] = 'Hands off! That is not your story.'
      redirect_back_or_default(stories_url)
    else
      super
    end
  end
    
  
end
